package com.origin.niuke.string;

/**
 * 二进制求和
 * 算法：模拟
 *
 * @author yezh
 * @date 2023/2/2 21:21
 */
public class NC188 {

    public String binaryAdd(String A, String B) {
        StringBuilder ans = new StringBuilder();
        int m = A.length() - 1, n = B.length() - 1, t = 0;
        while (m >= 0 || n >= 0) {
            int a = 0, b = 0;
            if (m >= 0) {
                a = A.charAt(m) - '0';
                --m;
            }
            if (n >= 0) {
                b = B.charAt(n) - '0';
                --n;
            }
            int sum = a + b + t;
            ans.append(sum % 2);
            t = sum / 2;
        }
        if (t == 1) ans.append(t);
        return ans.reverse().toString();
    }

}
